﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using AMS.UI.Forms;
using AMS.DataAccess.Trademark.Interfaces;
using AMS.DataAccess.Trademark.BO;
using AMS.ComponentModel;

namespace AMS.Trademark.Management
{
    public partial class FrmTaskRepository : BaseForm
    {
        public FrmTaskRepository()
        {
            InitializeComponent();
            ApplyDelayLoadData();
        }

        protected override void DelayLoadData()
        {
            Cursor = Cursors.WaitCursor;
            try
            {
                grid.Init("TaskRepository");
                DoDataBind();
            }
            finally
            {
                Cursor = Cursors.Default;
            }
        }

        private void DoDataBind()
        {
            IRFTaskManager rftask = ObjectUtility.GetObject<IRFTaskManager>("RFTask");
            List<TaskCategoryInfo> tasks = rftask.GetTasks(null);
            grid.DataBind(tasks);
            grid.RecordCount = tasks.Count;
        }

        private void grid_SelectedRowChanged(object sender, EventArgs e)
        {
            int dataRowIndex = grid.GridView.GetSelectedRowDataIndex();
            if (dataRowIndex == -1)
            {
                return;
            }

            List<TaskCategoryInfo> tasks = (List<TaskCategoryInfo>)grid.DataSource;
            TaskCategoryInfo task = tasks[dataRowIndex];
            ucTaskDetail1.DataBind(task);
        }

        private void btnRefresh_Click(object sender, EventArgs e)
        {
            DoDataBind();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            var dlg = new EditTaskDlg();
            if (DialogResult.OK == dlg.NewModel())
            {
                DoDataBind();
            }
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            if (grid.SelectedRow == null)
            {
                return;
            }

            int index = grid.GetSelectedRowDataIndex();
            List<TaskCategoryInfo> tasks = grid.DataSource as List<TaskCategoryInfo>;
            string rfIndex = tasks[index].TaskIndex;

            IEditDialog<TaskCategoryInfo> dlg = new EditTaskDlg();
            if (DialogResult.OK == dlg.EditModel(tasks[index]))
            {
                DoDataBind();
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            int []indices  = grid.GridView.GetCheckedDataRowIndices();
            if (indices == null || indices.Length == 0)
            {
                MessageBox.Show("请勾选要删除的行");
                return;
            }

            if (DialogResult.Yes != MessageBox.Show(this, "确认要删除选中的行吗？警告：删除的数据将不可恢复", "", MessageBoxButtons.YesNoCancel))
            {
                return;
            }

            IRFTaskManager rftask = ObjectUtility.GetObject<IRFTaskManager>("RFTask");
            List<TaskCategoryInfo> tasks = grid.DataSource as List<TaskCategoryInfo>;
            foreach (int index in indices)
            {
                // 暂时不启用删除
                rftask.DeleteTask(tasks[index].TaskIndex);
            }

            DoDataBind();
        }
    }
}
